#!/usr/bin/perl
#
# Once-off or thereabouts: move the country.story file into the
#database using the statistics table
#
use Counter;
use POSIX;

my $stats = Counter::openfile("statistics");
open(STORY, "temp/country.story") || die "No temp/country.story";
$nowdate = strftime "%Y-%m-%d %H:%M:%S", gmtime();
$lines = 0;
while (<STORY>) {
    chomp;
    my ($date, $country, $count) = split(/:/);
    my $stat = $stats->newnum();
    $stat->{name}= "persons";
    $stat->{date} = "$date 00:00:00";
    if ($country =~ /^..$/) {
	$stat->{qualifier} = "country=$country";
    } elsif ($country eq "All") {
	$stat->{qualifier}= '';
    } elsif ($country eq "Real") {
	$stat->{qualifier} = "All - freeze candidates";
    } else {
	warn "Unknown data in country field: $country\n";
    }
    $stat->{comment} = "Inserted from country.story file on $nowdate";
    $stat->{value} = $count;
    $stat->store();
    ++ $lines;
    print STDERR "$lines\r" if $lines % 100 == 0;
}
print "$lines lines entered\n";
